<?php

namespace app\rrb\model;

use org\BaiduMap;
use think\Db;
use think\Model;

class Make extends Model
{
    //protected $table = 'slzd_rrb_getlist';
    // 查看首页分类
    public function getListData($param)
    {
        if ($param['type'] == 1) {
            $data = $this->table('slzd_rrb_getlist')
                ->where('status', 1)
                ->where('pid', $param['pid'])
                ->order('sort asc')
                ->select();
            foreach ($data as &$v) {
                if ($v['image']) {
                    $v['image'] = config('path.admimg') . $v['image'];
                } else {
                    $v['image'] = '';
                }
            }

        } elseif ($param['type'] == 2) {
            $data['parent'] = $this->table('slzd_rrb_getlist')
                ->where('status', 1)
                ->where('pid', $param['pid'])
                ->select();
            if (!empty($data) && $data['parent'][0]['id']) {
                $data['children'] = $this->table('slzd_rrb_getlist')
                    ->where('status', 1)
                    ->where('pid', $data['parent'][0]['id'])
                    ->select();
            }
            foreach ($data['parent'] as &$v) {
                if ($v['image']) {
                    $v['image'] = config('path.admimg') . $v['image'];
                } else {
                    $v['image'] = '';
                }
            }
            foreach ($data['children'] as &$v) {
                if ($v['image']) {
                    $v['image'] = config('path.admimg') . $v['image'];
                } else {
                    $v['image'] = '';
                }
            }
        }
        return $data;
    }

    // 查看首页全部列表
    public function getIndexAllList($id)
    {
        $data = $this->table('slzd_rrb_getlist a')
            ->join('slzd_demand b')
            ->where('status', 1)
            ->where('id', $id)
            ->select();
        //echo $this->getLastSql();
        return $data;
    }

    // 查看首页详情
    public function getIndexDetailData($param)
    {
        $code = BaiduMap::getCodeByPoint($param['lng'], $param['lat']);
        if ($param['type'] == 1) {
            $field = [
                'a.id',
                'a.uid',
                'base_price',
                'nickname',
                'avatar',
                'category_name',
                'service_type',
                'service_time',
                'evaluationScore',
                'content',
                'type',
                'remark',
                'a.lng',
                'a.lat',
            ];

//            $i = config('distance'); //差值可自定义，值越大，范围就越大
//            $min_latitude = $param['lat'] - $i; //纬度最小值
//            $max_latitude = $param['lat'] + $i; //纬度最大值
//            $min_longitude = $param['lng'] - $i; //经度最小值
//            $max_longitude = $param['lng'] + $i; //经度最大值
            $data = Db::name('rrb_skill a')->join('slzd_user b', 'a.uid = b.id')
                //->join('slzd_rrb_getlist c','a.type = c.id')
                ->field($field)
                ->where('type', $param['id'])
//                ->where('a.lat','between',[$min_latitude,$max_latitude])
//                ->where('a.lng','between',[$min_longitude,$max_longitude])
                ->where(['a.status' => 1])->where(['del' => 1, 'a.citycode|a.adcode' => $code['citycode'], 'a.examine' => 2])->order('a.id desc')->select();
//        $data = $this->table('slzd_rrb_skill')
//            ->where('status',1)
//            ->where('del',1)
//            ->where('type',$param['id'])
//            ->select();
        } else {
            $field = [
                'a.id',
                'a.uid',
                'base_price',
                'nickname',
                'avatar',
                'category_name',
                'service_type',
                'service_time',
                'evaluationScore',
                'content',
                'remark',
            ];
//            $i = config('distance'); //差值可自定义，值越大，范围就越大
//            $min_latitude = $param['lat'] - $i; //纬度最小值
//            $max_latitude = $param['lat'] + $i; //纬度最大值
//            $min_longitude = $param['lng'] - $i; //经度最小值
//            $max_longitude = $param['lng'] + $i; //经度最大值
            $data = $this->table('slzd_rrb_skill a')->join('slzd_user b', 'a.uid = b.id')
                ->field($field)
                ->where('a.status', 1)
                ->where('del', 1)
//                ->where('a.lat','between',[$min_latitude,$max_latitude])
//                ->where('a.lng','between',[$min_longitude,$max_longitude])
                //->where('type',$param['id'])
                ->where('category_id', $param['id'])
                ->where(['a.citycode|a.adcode' => $code['citycode'], 'a.examine' => 2])
                ->select();
        }
        foreach ($data as &$value) {
            $value['content'] = unserialize($value['content']);
            $value['avatar'] = config('path.userimg') . $value['avatar'];
            $value['showprice'] = $value['base_price'] / 100;
            switch ($value['service_type']) {
                case 1 :
                    $value['service_type'] = '上门服务';
                    break;
                case 2 :
                    $value['service_type'] = '到店服务';
                    break;
                case 3 :
                    $value['service_type'] = '线上服务';
                    break;
            }
        }
//        foreach($data as &$value){
//            $value['content'] = unserialize($value['content']);
//            $value['show_price'] =  ($value['base_price'])/100;
//            switch ($value['service_type']){
//                case 1 :
//                    $value['service_type'] = '上门服务';
//                    break;
//                case 2 :
//                    $value['service_type'] = '到店服务';
//                    break;
//                case 3 :
//                    $value['service_type'] = '线上服务';
//                    break;
//            }
//        }
        //echo $this->getLastSql();

        return $data;
    }

    // 查看发布列表详情
    public function getDetailData($id)
    {
        $data = $this->table('slzd_rrb_getlist')
            ->where('status', 1)
            ->where('id', $id)
            ->order('sort desc')
            ->find();
        return $data;
    }

    // 查看发布列表详情
    public function getSkillDetailData($id)
    {
        $data = $this->table('slzd_rrb_getlist')
            ->where('status', 1)
            ->where('id', $id)
            ->order('sort desc')
            ->find();
        return $data;
    }
    // 查看首页详情
//    public function getTestData()
//    {
//        $oid = 18;
//        $data1 = $this->table('slzd_rrb_category')
//            //->where('status',1)
//            ->where('oid',$oid)
//            //->order('sort desc')
//            ->find();
//        $category = explode(',',$data1['category']);
//        $data = $this->table('slzd_rrb_getlist')
//            ->where('status',1)
//            ->wherein('id',$category)
//            //->order('sort desc')
//            ->select();
//        dump($data1);exit;
//        $data1 = [];
//        foreach ($data as $value) {
//
//            $data1[] = $value;
//        }
//        return $data1;
//    }
}